package com.hxy.travel.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.travel.constant.RedisConstant;
import com.hxy.travel.dao.OrderSettingDao;
import com.hxy.travel.dao.SetmealDao;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.pojo.OrderSetting;
import com.hxy.travel.pojo.Setmeal;
import com.hxy.travel.service.OrderSettingService;
import com.hxy.travel.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

     @Autowired
     private OrderSettingDao orderSettingDao;


    @Override
    public void addOrderSetting(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            int count = orderSettingDao.selectCountByDate(orderSetting.getOrderDate());
            if (count > 0){
                orderSettingDao.editNumberByDate(orderSetting);
            }else{
                orderSettingDao.addOrderSetting(orderSetting);
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date +"-1";
        String dateEnd = null;
        Integer month = Integer.parseInt(date.substring(date.lastIndexOf("-") + 1)) ;
        switch (month){
            case 2:
                dateEnd = date +"-28";
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dateEnd = date +"-31";
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dateEnd = date +"-30";
                break;

        }


        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(dateBegin,dateEnd);

        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map orderSettingMap = new HashMap();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String strDate = simpleDateFormat.format(orderSetting.getOrderDate());
            orderSettingMap.put("date", strDate.substring(strDate.lastIndexOf("-")+1) );
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void eidtOrderSetting(OrderSetting orderSetting) {
        int count = orderSettingDao.selectCountByDate(orderSetting.getOrderDate());

        if (count > 0){
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
            orderSettingDao.addOrderSetting(orderSetting);
        }

    }


//    @Override
//    public void addSetmealAndIds(Setmeal setmeal, Integer[] travelGroupIds) {
//
//        setmealDao.addSetmeal(setmeal);
//
//        String img=setmeal.getImg().substring(setmeal.getImg().lastIndexOf("/")+1);
//
//       jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
//
//        if (travelGroupIds.length > 0){
//
//            for (Integer travelGroupId : travelGroupIds) {
//                Map<String,Integer> map = new HashMap<>();
//                map.put("travelGroupId",travelGroupId);
//                map.put("setmealId",setmeal.getId());
//
//                setmealDao.addSetmealAndItem(map);
//            }
//        }
//    }
//
//
//
//    @Override
//    public PageResult findPage(QueryPageBean queryPageBean) {
//
//        Integer currentPage = queryPageBean.getCurrentPage() == null ? 1 : queryPageBean.getCurrentPage();
//
//        Integer pageSize = queryPageBean.getPageSize() == null ? 10 : queryPageBean.getPageSize();
//
//        PageHelper.startPage(currentPage,pageSize);
//
//
//        List<Setmeal> list = setmealDao.findPage(queryPageBean.getQueryString());
//
//        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
//
//        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
//    }
//
//    @Override
//    public Setmeal selectById(Integer id) {
//        return setmealDao.selectById(id);
//    }
//
//    @Override
//    public void editSetmeal(Setmeal setmeal, Integer[] travelgroupIds) {
//        setmealDao.editSetmeal(setmeal);
//
//        setmealDao.deleteSetmealAndGroupById(setmeal.getId());
//
//        if (travelgroupIds.length > 0){
//
//            for (Integer travelgroupId : travelgroupIds) {
//
//                Map<String,Integer> map = new HashMap<>();
//
//                map.put("travelgroupId",travelgroupId);
//
//                map.put("setmealId",setmeal.getId());
//
//                setmealDao.addSetmealAndGroup(map);
//
//            }
//        }
//    }
//
//    @Override
//    public TravelGroup selectById(Integer id) {
//        return travelGroupDao.selectById(id);
//    }
//
//    @Override
//    public void editTravelGroup(TravelGroup travelGroup, Integer[] travelItemIds) {
//        travelGroupDao.editTravelGroup(travelGroup);
//
//        travelGroupDao.deleteTravelGroupAndItemById(travelGroup.getId());
//
//        if (travelItemIds.length > 0){
//
//            for (Integer travelItemId : travelItemIds) {
//
//                Map<String,Integer> map = new HashMap<>();
//
//                map.put("travelItemId",travelItemId);
//
//                map.put("travelGroupId",travelGroup.getId());
//
//                travelGroupDao.addTravelGroupAndItem(map);
//
//            }
//        }
//    }
//
//    @Override
//    public void deleteTravelItemById(Integer id) {
//        int count = travelGroupDao.selectTravelItemByItemId(id);
//        if (count > 0){
//            throw new RuntimeException("数据关联不可删除！");
//        }
//        travelGroupDao.deleteTravelItemById(id);
//    }
//
//    @Override
//    public List<TravelGroup> findAll() {
//        return travelGroupDao.findAll();
//    }
//
//    @Override
//    public void deleteTravelItemById(Integer id) {
//        travelItemDao.deleteTravelItemById(id);
//    }
//
//    @Override
//    public TravelItem selectById(Integer id) {
//        return travelItemDao.selectById(id);
//    }
//
//    @Override
//    public void editTravelItem(TravelItem travelItem) {
//        travelItemDao.editTravelItem(travelItem);
//    }
//
//    @Override
//    public List<TravelItem> selectTravelItemList() {
//        return travelItemDao.selectAllTravelItemList();
//    }
}
